<?php

namespace App\Imports;

use App\Func\MeLawCaseFunc;
use App\Models\MeClient\MeClientModel;
use App\Models\MeLawCase\MeLawCase;
use App\Models\MeLawCase\MeLawCasePhoneModel;
use App\Tools\Log\Log;
use Maatwebsite\Excel\Concerns\ToCollection;
use Illuminate\Support\Collection;

class CasesImport implements ToCollection
{

    protected $clientId;
    protected $ownerId;

    public function __construct($client_id, $owner_id)
    {
        $this->clientId = $client_id;
        $this->ownerId = $owner_id;
    }

    public function collection(Collection $rows)
    {
        $data = [];
        $phoneModel = new MeLawCasePhoneModel();
        $clientInfo =  (new MeClientModel())->getByIdNoAuth($this->clientId,['client_import_count','client_code','client_custom_json','client_map_excel_file_column_header_to_database_table_field']);
        $client_import_count =  (int)$clientInfo['client_import_count'];
        $law_case_batch_number = $client_import_count + 1;
        foreach ($rows as $k => $row) {
            if ($k === 0) {
                continue;
            }
            $phone = $phoneModel->select(['law_case_phone_id'])->where('phone_number', $row[0])->first();
            if ($phone) {
                $phoneId = $phone['law_case_phone_id'];
            } else {
                $phoneId = $phoneModel->withoutGlobalScopes()->insertGetId([
                    'phone_number' => $row[0],
                    'phone_status' => 1,
                    'is_wd' => 2,
                    'created_time' => date('Y-m-d H:i:s'),
                    'updated_time' => date('Y-m-d H:i:s'),
                ]);
            }
            $data[] = [
                'main_respondent_phone_id' => $phoneId,
                'respondent_phone_ids' => $phoneId,
                'client_id' => $this->clientId,
                'owner_id' => $this->ownerId,
                'respondent_name' => $row[3],
                'respondent_id_card' => $row[2],
                'law_case_batch_number' => $law_case_batch_number,
                'law_case_code' => MeLawCaseFunc::getLawCaseUnionCode($this->clientId, date("ymdhis")),
                'law_case_ex' => json_encode([
                    '分公司' => $row[1] ?? '',
                    '身份证号码' => $row[2] ?? '',
                    '姓名' => $row[3] ?? '',
                    '借款人姓名' => $row[4] ?? '',
                    '借款合同号' => $row[5] ?? '',
                    '开户时间' => $row[8] ?? '',
                    '授信额度（元）' => $row[9] ?? '',
                    '贷款金额' => $row[10] ?? '',
                    '币种' => $row[11] ?? '',
                    '贷款发放日' => $row[12] ?? '',
                    '贷款到期日' => $row[13] ?? '',
                    '贷款期数' => $row[14] ?? '',
                    '剩余期限' => $row[15] ?? '',
                    '还款来源' => $row[16] ?? '',
                    '还款方式' => $row[17] ?? '',
                    '产品种类' => $row[18] ?? '',
                    '利率类型（固定/浮动）' => $row[19] ?? '',
                    '截至基准日执行利率' => $row[20] ?? '',
                    '担保方式' => $row[21] ?? '',
                    '贷款用途' => $row[22] ?? '',
                    '借款人所属地区' => $row[23] ?? '',
                    '借款人类型（企业/个人）' => $row[24] ?? '',
                    '借款人所属地区（省级行政区）' => $row[25] ?? '',
                    '借款人所在地区（市）' => $row[26] ?? '',
                    '最后一次不良的时间' => $row[27] ?? '',
                    '进入不良时本金余额' => $row[28] ?? '',
                    '进入不良时利息费用' => $row[29] ?? '',
                    '截至封包日未偿本金余额（元）' => $row[30] ?? '',
                    '未偿利息' => $row[31] ?? '',
                    '本+利' => $row[32] ?? '',
                    '未偿复利' => $row[33] ?? '',
                    '本金+利息+复利' => $row[34] ?? '',
                    '逾期天数' => $row[35] ?? '',
                    '逾期原因' => $row[36] ?? '',
                    '年龄' => $row[37] ?? '',
                    '性别' => $row[38] ?? '',
                    '职业' => $row[39] ?? '',
                    '学历' => $row[40] ?? '',
                    '年收入' => $row[41] ?? '',
                    '其他贷余额（如有）' => $row[42] ?? '',
                    '最近三个月人行征信查询次数' => $row[43] ?? '',
                    '婚姻状况' => $row[44] ?? '',
                    '事业部' => $row[45] ?? '',
                    '管理中心' => $row[46] ?? '',
                    '产品名称' => $row[47] ?? '',
                    '是否受托支付' => $row[48] ?? '',
                    '五级分类标志' => $row[49] ?? '',
                    '核销标识' => $row[50] ?? '',
                    '核销时间' => $row[51] ?? '',
                    '核销时剩余本金' => $row[52] ?? '',
                    '是否抵押贷' => $row[53] ?? '',
                    '门店' => $row[54] ?? '',
                    '资产标签' => $row[56] ?? '',
                    '资产来源' => $row[57] ?? '',
                    '诉讼仲裁案号' => $row[58] ?? ''
                ])
            ];
        }
        $caseModel = new MeLawCase();
        $caseModel->withoutGlobalScopes()->insertOrIgnore($data);
        return true;
    }


    public function rules(): array
    {
        return [];
        return [
            '*.email' => 'email|unique:users,email',
            '*.name' => 'required|min:2',
        ];
    }

    public function batchSize(): int
    {
        return 1000; // 每1000条记录批量插入
    }

    public function chunkSize(): int
    {
        return 1000; // 每次读取1000行
    }
}